package com.data.core.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.common.pojo.Result;
import com.common.vo.FileInfoVo;
import com.common.vo.res.FileInfoRes;
import com.data.core.service.FileInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import com.data.core.entry.FileInfo;

import javax.annotation.Resource;

@Api(value = "文件上传相关API")
@RestController
@RequestMapping("file")
public class FileInfoController {

    @Resource
    private FileInfoService fileInfoService;

    @ApiOperation(value = "文件上传",notes = "文件上传")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "files",value = "多个文件，",paramType = "formData",allowMultiple=true,required = true,dataType = "file"),
            @ApiImplicitParam(name = "fileName",value = "文件名称",paramType = "query",required = true,dataType = "String"),
            @ApiImplicitParam(name = "fileType",value = "文件类型",paramType = "query",required = true,dataType = "int"),
            @ApiImplicitParam(name = "created",value = "上传者",paramType = "query",required = true,dataType = "String"),
            @ApiImplicitParam(name = "enable",value = "是否启用",paramType = "query",required = true,dataType = "int"),
    })
    @PostMapping(value = "upload",headers = "content-type=multipart/form-data")
    public Result<?> upload(@RequestParam(value = "fileName") String fileName,
                            @RequestParam(value = "fileType") Integer fileType,
                            @RequestParam(value = "created") String created,
                            @RequestParam(value = "enable") Integer enable,
                            @RequestParam(value = "files") MultipartFile[] files) {
        Result result = new Result();
        FileInfoRes fileInfoRes =  FileInfoRes.builder().fileName(fileName).fileType(fileType).created(created).enable(enable).build();
        result.OK(fileInfoService.upload(fileInfoRes,files));
        return result;
    }

    @ApiOperation(value = "查询文件信息",notes = "查询文件信息")
    @ApiImplicitParam(name = "id", value = "附件id", required = true, dataType = "Long")
    @PostMapping("find")
    public Result<?> findFileInfo(@RequestBody FileInfoVo fileInfoVo) throws Exception {
        Result result = new Result();
        FileInfoRes fileInfoRes =  FileInfoRes.builder().id(fileInfoVo.getId()).build();
        result.OK(fileInfoService.findFile(fileInfoRes));
        return result;
    }

    @ApiOperation(value = "删除附件",notes = "删除附件")
    @ApiImplicitParam(name = "id", value = "附件id", required = true, dataType = "Long")
    @GetMapping("delete")
    public Result<?> delete(@RequestParam Long id) throws Exception {
        Result result = new Result();
        result.OK(fileInfoService.delete(id));
        return result;
    }

    @ApiOperation(value = "附件列表",notes = "附件列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页数", paramType = "query",required = true, dataType = "int"),
            @ApiImplicitParam(name = "pageSize",value = "条数",paramType = "query",required = true,dataType = "int"),
    })
    @DeleteMapping("delete")
    public Result<?> pageFileInfo(@RequestBody FileInfoRes fileInfoRes,
                                  @RequestParam(value = "pageNum") int pageNum,
                                  @RequestParam(value = "pageSize")  int pageSize) throws Exception {
        Result result = new Result();
        FileInfo fileInfo =new FileInfo();
        BeanUtils.copyProperties(fileInfoRes,fileInfo);
        Page<FileInfo> page = new Page<>(pageNum,pageSize);
        result.OK(fileInfoService.findFileInfoVos(page,fileInfoRes));
        return result;
    }
}
